"""
Case Type   : 兼容M*数据库timestampadd函数
Case Name   : timestampadd函数，第3个参数是datetime，部分测试点合理报错
Create At   : 2023.04.07
Owner       : z@wan005
Description :
    1、入参依次为year，整数，datetime类型
    2、入参依次为qtr，整数，datetime类型
    3、入参依次为month，整数，datetime类型
    4、入参依次为week，整数，datetime类型
    5、入参依次为day，整数，datetime类型
    6、入参依次为hour，整数，datetime类型
    7、入参依次为minute，整数，datetime类型
    8、入参依次为second，整数，datetime类型
    9、入参依次为microsecond，整数，datetime类型
    10、返回值超范围
    11、清理环境
Expect      :
    1、成功
    2、成功
    3、成功
    4、成功
    5、成功
    6、成功
    7、成功
    8、成功
    9、成功
    10、弹出告警返回null
    11、成功
History     :modified by zhanghuan96,2024.7.3，step10同M8.0结果保持一致
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class CompatibilityTest03(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.common = Common()
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.db_name = "db_timestampadd_0003"

    def test_subdate(self):
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.log.info('----前置条件：创建兼容b库---')
        sql_cmd = f"drop database if exists {self.db_name};" \
                  f"create database {self.db_name} dbcompatibility 'b';"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, sql_res)

        text = '--step1:入参依次为year，整数，datetime类型;expect:成功--'
        self.log.info(text)
        sql_cmd = "select timestampadd(year, 1, '0000-01-01 00:00:00');" \
                  "select timestampadd(year, 1, '0001-01-01 23:59:59');" \
                  "select timestampadd(year, 1, '2016-2-29 08:00:00');" \
                  "select timestampadd(year, 1, '99981231235959');" \
                  "select timestampadd(year, 1.4, '99981231235959');" \
                  "select timestampadd(year, 1.5, '99971231235959');"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=self.db_name)
        self.log.info(sql_res)
        self.assertTrue(len(sql_res.strip().splitlines()) >= 24, '行数不到24行')
        self.assertEqual('0001-01-01 00:00:00',
                         sql_res.splitlines()[2].strip(), '执行失败' + text)
        self.assertEqual('0002-01-01 23:59:59',
                         sql_res.splitlines()[7].strip(), '执行失败' + text)
        self.assertEqual('2017-02-28 08:00:00',
                         sql_res.splitlines()[12].strip(), '执行失败' + text)
        self.assertEqual('9999-12-31 23:59:59',
                         sql_res.splitlines()[17].strip(), '执行失败' + text)
        self.assertEqual('9999-12-31 23:59:59',
                         sql_res.splitlines()[22].strip(), '执行失败' + text)
        self.assertEqual('9999-12-31 23:59:59',
                         sql_res.splitlines()[-2].strip(), '执行失败' + text)

        text = '--step2:入参依次为qtr，整数，datetime类型;expect:成功--'
        self.log.info(text)
        sql_cmd = "select timestampadd(qtr, 1, '0000-01-01 00:00:00');" \
                  "select timestampadd(qtr, 1, '0001-01-01 23:59:59');" \
                  "select timestampadd(qtr, 1, '2016-2-29 08:00:00');" \
                  "select timestampadd(qtr, 1, '99981231235959');" \
                  "select timestampadd(qtr, 4.4, '99981231235959');" \
                  "select timestampadd(qtr, 4.5, '99971231235959');"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=self.db_name)
        self.log.info(sql_res)
        self.assertTrue(len(sql_res.strip().splitlines()) >= 24, '行数不到24行')
        self.assertEqual('0000-04-01 00:00:00',
                         sql_res.splitlines()[2].strip(), '执行失败' + text)
        self.assertEqual('0001-04-01 23:59:59',
                         sql_res.splitlines()[7].strip(), '执行失败' + text)
        self.assertEqual('2016-05-29 08:00:00',
                         sql_res.splitlines()[12].strip(), '执行失败' + text)
        self.assertEqual('9999-03-31 23:59:59',
                         sql_res.splitlines()[17].strip(), '执行失败' + text)
        self.assertEqual('9999-12-31 23:59:59',
                         sql_res.splitlines()[22].strip(), '执行失败' + text)
        self.assertEqual('9999-03-31 23:59:59',
                         sql_res.splitlines()[-2].strip(), '执行失败' + text)

        text = '--step3:入参依次为month，整数，datetime类型;expect:成功--'
        self.log.info(text)
        sql_cmd = "select timestampadd(month, 1, '0000-01-01 14:25:59');" \
                  "select timestampadd(month, 1, '0001-01-01 01:01:01');" \
                  "select timestampadd(month, 1, '2016-2-29 23:59:59');" \
                  "select timestampadd(month, 1, '99981231235959');" \
                  "select timestampadd(month, 12.4, '99981231235959');" \
                  "select timestampadd(month, 12.5, '99971231235959');"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=self.db_name)
        self.log.info(sql_res)
        self.assertTrue(len(sql_res.strip().splitlines()) >= 24, '行数不到24行')
        self.assertEqual('0000-02-01 14:25:59',
                         sql_res.splitlines()[2].strip(), '执行失败' + text)
        self.assertEqual('0001-02-01 01:01:01',
                         sql_res.splitlines()[7].strip(), '执行失败' + text)
        self.assertEqual('2016-03-29 23:59:59',
                         sql_res.splitlines()[12].strip(), '执行失败' + text)
        self.assertEqual('9999-01-31 23:59:59',
                         sql_res.splitlines()[17].strip(), '执行失败' + text)
        self.assertEqual('9999-12-31 23:59:59',
                         sql_res.splitlines()[22].strip(), '执行失败' + text)
        self.assertEqual('9999-01-31 23:59:59',
                         sql_res.splitlines()[-2].strip(), '执行失败' + text)

        text = '--step4:入参依次为week，整数，datetime类型;expect:成功--'
        self.log.info(text)
        sql_cmd = "select timestampadd(week, 1, '0001-01-01 01:01:01');" \
                  "select timestampadd(week, 1, '2016-2-29 23:59:59');" \
                  "select timestampadd(week, 1, '99991224235959');" \
                  "select timestampadd(week, 1.99, '99991217235959');"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=self.db_name)
        self.log.info(sql_res)
        msg = sql_res.strip().splitlines()
        self.log.info(msg)
        self.assertTrue(len(sql_res.strip().splitlines()) >= 16, '行数不到16行')
        self.assertEqual('0001-01-08 01:01:01',
                         sql_res.splitlines()[2].strip(), '执行失败' + text)
        self.assertEqual('2016-03-07 23:59:59',
                         sql_res.splitlines()[7].strip(), '执行失败' + text)
        self.assertEqual('9999-12-31 23:59:59',
                         sql_res.splitlines()[12].strip(), '执行失败' + text)
        self.assertEqual('9999-12-31 23:59:59',
                         sql_res.splitlines()[-2].strip(), '执行失败' + text)

        text = '--step5:入参依次为day，整数，datetime类型;expect:成功--'
        self.log.info(text)
        sql_cmd = "select timestampadd(day, 1, '0001-01-01 23:59:59');" \
                  "select timestampadd(day, 1, '2016-2-29 23:59:59');" \
                  "select timestampadd(day, 1, '99991230225959');" \
                  "select timestampadd(day, 1.4, '99991230185959');" \
                  "select timestampadd(day, 1.55, '99991229185959');"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=self.db_name)
        self.log.info(sql_res)
        msg = sql_res.strip().splitlines()
        self.log.info(msg)
        self.assertTrue(len(sql_res.strip().splitlines()) >= 20, '行数不到20行')
        self.assertEqual('0001-01-02 23:59:59',
                         sql_res.splitlines()[2].strip(), '执行失败' + text)
        self.assertEqual('2016-03-01 23:59:59',
                         sql_res.splitlines()[7].strip(), '执行失败' + text)
        self.assertEqual('9999-12-31 22:59:59',
                         sql_res.splitlines()[12].strip(), '执行失败' + text)
        self.assertEqual('9999-12-31 18:59:59',
                         sql_res.splitlines()[17].strip(), '执行失败' + text)
        self.assertEqual('9999-12-31 18:59:59',
                         sql_res.splitlines()[-2].strip(), '执行失败' + text)

        text = '--step6:入参依次为hour，整数，datetime类型;expect:成功--'
        self.log.info(text)
        sql_cmd = "select timestampadd(hour, 24, '0001-01-01 01:01:01');" \
                  "select timestampadd(hour, 23.9, '2016-2-29 01:01:01');" \
                  "select timestampadd(hour, 23.4, '2016-2-29 00:01:01');" \
                  "select timestampadd(hour, 47, '99991230000000');" \
                  "select timestampadd(hour, 47.4, '99991230000000');"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=self.db_name)
        self.log.info(sql_res)
        msg = sql_res.strip().splitlines()
        self.log.info(msg)
        self.assertTrue(len(sql_res.strip().splitlines()) >= 20, '行数不到20行')
        self.assertEqual('0001-01-02 01:01:01',
                         sql_res.splitlines()[2].strip(), '执行失败' + text)
        self.assertEqual('2016-03-01 01:01:01',
                         sql_res.splitlines()[7].strip(), '执行失败' + text)
        self.assertEqual('2016-02-29 23:01:01',
                         sql_res.splitlines()[12].strip(), '执行失败' + text)
        self.assertEqual('9999-12-31 23:00:00',
                         sql_res.splitlines()[17].strip(), '执行失败' + text)
        self.assertEqual('9999-12-31 23:00:00',
                         sql_res.splitlines()[-2].strip(), '执行失败' + text)

        text = '--step7:入参依次为minute，整数，datetime类型;expect:成功'
        self.log.info(text)
        sql_cmd = "select timestampadd(minute, 59, '0001-01-01 23:59:59');" \
                  "select timestampadd(minute, 59.9, '2016-2-29 08:00:00');" \
                  "select timestampadd(minute, 23.4, '2016-2-29 10:00:00');" \
                  "select timestampadd(minute, 2820, '99991230000059');" \
                  "select timestampadd(minute, 2820.4444, '99991230000000');"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=self.db_name)
        self.assertTrue(len(sql_res.strip().splitlines()) >= 20, '行数不到20行')
        self.log.info(sql_res)
        msg = sql_res.strip().splitlines()
        self.log.info(msg)
        self.assertEqual('0001-01-02 00:58:59',
                         sql_res.splitlines()[2].strip(), '执行失败' + text)
        self.assertEqual('2016-02-29 09:00:00',
                         sql_res.splitlines()[7].strip(), '执行失败' + text)
        self.assertEqual('2016-02-29 10:23:00',
                         sql_res.splitlines()[12].strip(), '执行失败' + text)
        self.assertEqual('9999-12-31 23:00:59',
                         sql_res.splitlines()[17].strip(), '执行失败' + text)
        self.assertEqual('9999-12-31 23:00:00',
                         sql_res.splitlines()[-2].strip(), '执行失败' + text)

        text = '--step8:入参依次为second，整数，datetime类型;expect:成功--'
        self.log.info(text)
        sql_cmd = "select timestampadd(second, 59, '0001-01-01 23:59:59');" \
                  "select timestampadd(second, 59.9, '0001-01-01 23:59:59');" \
                  "select timestampadd" \
                  "(second, 59.9999999, '0001-01-01 23:59:59');" \
                  "select timestampadd" \
                  "(second, 59.9, '2016-2-29 23:59:59.999999');" \
                  "select timestampadd" \
                  "(second, 23.4444447, '2016-2-29 23:59:59');" \
                  "select timestampadd(second, 82800, '99991231000001');"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=self.db_name)
        self.assertTrue(len(sql_res.strip().splitlines()) >= 24, '行数不到24行')
        self.log.info(sql_res)
        self.assertEqual('0001-01-02 00:00:58',
                         sql_res.splitlines()[2].strip(), '执行失败' + text)
        self.assertEqual('0001-01-02 00:00:58.9',
                         sql_res.splitlines()[7].strip(), '执行失败' + text)
        self.assertEqual('0001-01-02 00:00:58.999999',
                         sql_res.splitlines()[12].strip(), '执行失败' + text)
        self.assertEqual('2016-03-01 00:00:59.899999',
                         sql_res.splitlines()[17].strip(), '执行失败' + text)
        self.assertEqual('2016-03-01 00:00:22.444444',
                         sql_res.splitlines()[22].strip(), '执行失败' + text)
        self.assertEqual('9999-12-31 23:00:01',
                         sql_res.splitlines()[-2].strip(), '执行失败' + text)

        text = '--step9:入参依次为microsecond，整数，datetime类型;expect:成功--'
        self.log.info(text)
        sql_cmd = "select timestampadd" \
                  "(microsecond, 59, '0001-01-01 23:59:59');" \
                  "select " \
                  "timestampadd(microsecond, 59.9, '0001-01-01 23:59:59');" \
                  "select timestampadd" \
                  "(microsecond, 59.999999, '0001-01-01 23:59:59');" \
                  "select timestampadd" \
                  "(microsecond, 59.9999999, '2016-2-29 23:59:59');" \
                  "select " \
                  "timestampadd(microsecond, 4968000, '99991231000056');"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=self.db_name)
        self.assertTrue(len(sql_res.strip().splitlines()) >= 20, '行数不到20行')
        self.log.info(sql_res)
        self.assertEqual('0001-01-01 23:59:59.000059',
                         sql_res.splitlines()[2].strip(), '执行失败' + text)
        self.assertEqual('0001-01-01 23:59:59.00006',
                         sql_res.splitlines()[7].strip(), '执行失败' + text)
        self.assertEqual('0001-01-01 23:59:59.00006',
                         sql_res.splitlines()[12].strip(), '执行失败' + text)
        self.assertEqual('2016-02-29 23:59:59.00006',
                         sql_res.splitlines()[17].strip(), '执行失败' + text)
        self.assertEqual('9999-12-31 00:01:00.968',
                         sql_res.splitlines()[-2].strip(), '执行失败' + text)

        text = '--step10:返回值超范围;expect:成功，与M*包池一致--'
        self.log.info(text)
        sql_cmd = "select timestampadd(week, 1, '0000-01-01 14:25:59');" \
                  "select timestampadd(day, 1, '0000-01-01 23:59:59');" \
                  "select timestampadd(hour, 1, '0000-01-01 01:01:01');" \
                  "select timestampadd(minute, 1, '0000-01-01');"
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=self.db_name)
        self.log.info(sql_res)
        self.assertEqual(4, sql_res.count('0000-00-00'),
                         '执行失败' + text)

    def tearDown(self):
        text = '----step11:清理环境;expect:成功----'
        self.log.info(text)
        sql_cmd = f"drop database {self.db_name};"
        self.log.info(sql_cmd)
        drop_res1 = self.sh_primary.execut_db_sql(sql_cmd,
                                                  dbname='postgres')
        self.log.info(drop_res1)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, drop_res1,
                      '执行失败' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
